package com.core.service.xml;

import java.io.*;

import org.dom4j.*;
import org.dom4j.io.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * XML 操作服务类，使用该类需要继承，并实现 createDocument(Object object)方法
 * 
 * @author 刘威
 * 
 */
public abstract class XmlService {

	private static Log log = LogFactory.getLog(XmlService.class);

	/**
	 * 将 XML 文档写入文件
	 * 
	 * @param doc
	 * @param dir
	 * @return
	 */
	public boolean writeDocument(Document doc, String dir) {
		boolean ret = false;
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			FileOutputStream fos = new FileOutputStream(dir);
			XMLWriter writer = new XMLWriter(fos, format);
			writer.write(doc);
			writer.close();
			ret = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public abstract Document createDocument(Object object);
}
